package moniCA::LOG::Appender::MailReportAttachment;

# By: Joseph Annino
# (c) 2002
# Licensed under the same terms as Perl
#

use strict;
use warnings;
use moniCA::LOG::Dispatch::Email;
use List::MoreUtils qw(uniq);
use base qw( moniCA::LOG::Dispatch::Email );
use Data::Dumper;
use Mail::Sender;

sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;

    my %p    = @_;
    my $smtp = delete $p{smtp} || 'localhost';

    my $self = $class->SUPER::new(%p);

    $self->{smtp} = $smtp;
    return $self;
}

sub send_email {
    my $self = shift;
    my %p    = @_;
    local $?;
    eval {
        my $sender = Mail::Sender->new(
            {
                from    => $self->{from} || 'LogDispatch@foo.bar',
                replyto => $self->{from} || 'LogDispatch@foo.bar',
                smtp    => $self->{smtp},
                cc => ( join ',', @{ $self->{cc} } ),
                auth => $self->{auth},
                authid => $self->{authid},
                authpwd => $self->{authpwd},
                TLS_allowed => $self->{TLS_allowed},
            }
        );

        die "Error sending mail ($sender): $Mail::Sender::Error"
          unless ref $sender;

        ref $sender->OpenMultipart(
            {
                to       => ( join ',', @{ $self->{to} } ),
                subject  => $self->{subject},
                ctype    => 'text/html',
                encoding => '8bit'
            }
          )
          or die $Mail::Sender::Error, "\n";

        $sender->Part(
            {
                ctype       => 'text/html',
                disposition => 'NONE',
                msg         => '
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
  </head>
  <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;margin: 0;padding: 0;height: 100% !important;width: 100% !important;background-color: #DEE0E2">
     <center>
         <table align="center" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;border-collapse: collapse !important;height: 100% !important;margin: 0;padding: 0;width: 100% !important;background-color: #DEE0E2"><tr><td align="center" valign="top" id="bodyCell" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;height: 100% !important;margin: 0;padding: 20px;width: 100% !important;border-top: 4px solid #BBB">
                     <!-- BEGIN TEMPLATE // -->
                     <table border="0" cellpadding="0" cellspacing="0" id="templateContainer" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;border-collapse: collapse !important;width: 600px;border: 1px solid #BBB"><tr><td align="center" valign="top" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0">
                                 <!-- BEGIN PREHEADER // -->
                                    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templatePreheader" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;border-collapse: collapse !important;background-color: #F4F4F4;border-bottom: 1px solid #CCC"><tr><td valign="top" class="preheaderContent" style="padding-top: 10px;padding-right: 20px;padding-bottom: 10px;padding-left: 20px;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;color: #808080;font-family: Helvetica;font-size: 10px;line-height: 125%;text-align: left" mc:edit="preheader_content00">
                                                E-Mail notification of moniCA monitoring events.
                                            </td>
                                            <!-- *|IFNOT:ARCHIVE_PAGE|* -->
                                            <td valign="top" width="180" class="preheaderContent" style="padding-top: 10px;padding-right: 20px;padding-bottom: 10px;padding-left: 0;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;color: #808080;font-family: Helvetica;font-size: 10px;line-height: 125%;text-align: left" mc:edit="preheader_content01">
                                            </td>
                                            <!-- *|END:IF|* -->
                                        </tr></table><!-- // END PREHEADER --></td>
                            </tr><tr><td align="center" valign="top" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0">
                                 <!-- BEGIN HEADER // -->
                                    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateHeader" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;border-collapse: collapse !important;background-color: #F4F4F4;border-top: 1px solid #FFF;border-bottom: 1px solid #CCC"><tr><td valign="top" class="headerContent" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;color: #505050;font-family: Helvetica;font-size: 20px;font-weight: bold;line-height: 100%;padding-top: 0;padding-right: 0;padding-bottom: 0;padding-left: 0;text-align: left;vertical-align: middle">
                                             <img src="cid:header.jpg" style="max-width: 600px;-ms-interpolation-mode: bicubic;border: 0;height: auto;line-height: 100%;outline: none;text-decoration: none" id="headerImage" mc:label="header_image" mc:edit="header_image" mc:allowdesigner="" mc:allowtext="" /></td>
                                        </tr></table><!-- // END HEADER --></td>
                            </tr><tr><td align="center" valign="top" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0">
                                 <!-- BEGIN BODY // -->
                                    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateBody" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;border-collapse: collapse !important;background-color: #F4F4F4;border-top: 1px solid #FFF;border-bottom: 1px solid #CCC"><tr><td valign="top" class="bodyContent" mc:edit="body_content" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;color: #505050;font-family: Helvetica;font-size: 14px;line-height: 150%;padding-top: 20px;padding-right: 20px;padding-bottom: 20px;padding-left: 20px;text-align: left">

<h2 style="color: #404040 !important;display: block;font-family: Helvetica;font-size: 20px;font-style: normal;font-weight: bold;line-height: 100%;letter-spacing: normal;margin-top: 0;margin-right: 0;margin-bottom: 10px;margin-left: 0;text-align: left">'
                  . $self->{subject} . '</h2>

                                                <h3 style="color: #606060 !important;display: block;font-family: Helvetica;font-size: 16px;font-style: italic;font-weight: normal;line-height: 100%;letter-spacing: normal;margin-top: 0;margin-right: 0;margin-bottom: 10px;margin-left: 0;text-align: left">Description</h3>
' . $self->{DetailDesc} . '
                                                <br /><br /><h2 style="color: #404040 !important;display: block;font-family: Helvetica;font-size: 20px;font-style: normal;font-weight: bold;line-height: 100%;letter-spacing: normal;margin-top: 0;margin-right: 0;margin-bottom: 10px;margin-left: 0;text-align: left">Event Message</h2>
                                                <h4 style="color: #808080 !important;display: block;font-family: Helvetica;font-size: 14px;font-style: italic;font-weight: normal;line-height: 100%;letter-spacing: normal;margin-top: 0;margin-right: 0;margin-bottom: 10px;margin-left: 0;text-align: left">Details</h4>

<style>.WARN{background-color:#FFE600;color:#000000; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid;}</style>
<style>.FATAL{background-color:#000000;color:#FFFFFF; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid;}</style>
<style>.ERROR{background-color:#FF0000;color:#000000; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid;}</style>
<style>.INFO{background-color:#AADD00;color:#000000; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid #000000;}</style>
<style>.DEBUG{background-color:#9B30FF;color:#FFFFFF; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid #000000;}</style>
<style>.TRACE{background-color:#603311;color:#FFFFFF; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid #000000;}</style>

' . $p{message} . '

                                            </td>
                                        </tr>
</table><!-- // END BODY --></td>
                            </tr><tr><td align="center" valign="top" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0">
                                 <!-- BEGIN FOOTER // -->
                                    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateFooter" style="-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;border-collapse: collapse !important;background-color: #F4F4F4;border-top: 1px solid #FFF">
                                        <tr><td valign="top" class="footerContent" style="padding-top: 0;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;mso-table-lspace: 0;mso-table-rspace: 0;color: #808080;font-family: Helvetica;font-size: 10px;line-height: 150%;padding-right: 20px;padding-bottom: 20px;padding-left: 20px;text-align: left" mc:edit="footer_content01">

[cEvent=ClearEvent],[iEvent=InfoEvent],[tEvent=TriggerEvent]<br><br>

                                                <em>Copyright @2013 Andre Schmitt, All rights reserved.</em>
                                                <br />
                                            </td>
                                        </tr></table><!-- // END FOOTER --></td>
                            </tr></table><!-- // END TEMPLATE --></td>
                </tr></table></center>
    </body>
</html>

'
            }
        );

        $sender->Attach(
            {
                description => 'header.jpg',
                ctype       => 'image/jpeg',
                encoding    => 'base64',
                disposition => 'inline; filename="header.jpg"',
                content_id  => 'header.jpg',
                file        => $ENV{'MONICA_PRODUCT_DIR'}
                  . '/lib/moniCA/LOG/Appender/header.jpg'
            }
        );

        if ( -f $ENV{'MONICA_TMP_DIR'} . '/' . $self->{plug} . '.xml' ) {

            $sender->Attach(
                {
                    description => $self->{plug} . '.xml',
                    ctype       => 'text/xml',
                    encoding    => 'base64',
                    content_id  => $self->{plug} . '.xml',
                    file        => $ENV{'MONICA_TMP_DIR'} . '/'
                      . $self->{plug} . '.xml'
                }
            );
        }

        foreach ( uniq( @{ $self->{identifier} } ) ) {

            ( my $rrdident = $_ ) =~ s/\W/_/g;

            if (  -f $ENV{MONICA_VAR_DIR}
                . '/images/'
                . $self->{plug} . '_'
                . $rrdident
                . '.png' )
            {
                $sender->Attach(
                    {
                        description => $self->{plug} . '_' . $rrdident . '.png',
                        ctype       => 'image/png',
                        encoding    => 'base64',
                        content_id  => $self->{plug} . '_' . $rrdident . '.png',
                        file        => $ENV{MONICA_VAR_DIR}
                          . '/images/'
                          . $self->{plug} . '_'
                          . $rrdident . '.png',
                    }
                );
            }
        }
        $sender->Close();

    };

    warn $@ if $@;
}

1;

# ABSTRACT: Subclass of Log::Dispatch::Email that uses the Mail::Sender module

=pod

=head1 NAME

Log::Dispatch::Email::MailSender - Subclass of Log::Dispatch::Email that uses the Mail::Sender module

=head1 VERSION

version 2.29

=head1 SYNOPSIS

  use Log::Dispatch;

  my $log = Log::Dispatch->new(
      outputs => [
          [
              'Email::MailSender',
              min_level => 'emerg',
              to        => [qw( foo@example.com bar@example.org )],
              subject   => 'Big error!'
          ]
      ],
  );

  $log->emerg("Something bad is happening");

=head1 DESCRIPTION

This is a subclass of L<Log::Dispatch::Email> that implements the send_email
method using the L<Mail::Sender> module.

=head1 AUTHOR

Dave Rolsky <autarch@urth.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2011 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut

__END__

